/*     */ package com.zimbra.cs.service.formatter;
/*     */ 
/*     */ import com.zimbra.common.calendar.ZCalendar.ZCalendarBuilder;
/*     */ import com.zimbra.common.calendar.ZCalendar.ZICalendarParseHandler;
/*     */ import com.zimbra.common.calendar.ZCalendar.ZVCalendar;
/*     */ import com.zimbra.common.localconfig.KnownKey;
/*     */ import com.zimbra.common.localconfig.LC;
/*     */ import com.zimbra.common.service.ServiceException;
/*     */ import com.zimbra.common.util.FileBufferedWriter;
/*     */ import com.zimbra.common.util.HttpUtil;
/*     */ import com.zimbra.common.util.HttpUtil.Browser;
/*     */ import com.zimbra.cs.mailbox.CalendarItem;
/*     */ import com.zimbra.cs.mailbox.CalendarItem.Instance;
/*     */ import com.zimbra.cs.mailbox.Folder;
/*     */ import com.zimbra.cs.mailbox.MailItem;
/*     */ import com.zimbra.cs.mailbox.MailItem.Type;
/*     */ import com.zimbra.cs.mailbox.Mailbox;
/*     */ import com.zimbra.cs.mailbox.OperationContext;
/*     */ import com.zimbra.cs.mailbox.calendar.IcsImportParseHandler;
/*     */ import com.zimbra.cs.mailbox.calendar.IcsImportParseHandler.ImportInviteVisitor;
/*     */ import com.zimbra.cs.mailbox.calendar.Invite;
/*     */ import com.zimbra.cs.mime.Mime;
/*     */ import com.zimbra.cs.service.UserServletContext;
/*     */ import com.zimbra.cs.service.UserServletException;
/*     */ import java.io.IOException;
/*     */ import java.io.InputStream;
/*     */ import java.util.ArrayList;
/*     */ import java.util.Collection;
/*     */ import java.util.EnumSet;
/*     */ import java.util.Iterator;
/*     */ import java.util.List;
/*     */ import java.util.Set;
/*     */ import javax.servlet.ServletException;
/*     */ import javax.servlet.http.HttpServletRequest;
/*     */ import javax.servlet.http.HttpServletResponse;
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ public class IcsFormatter
/*     */   extends Formatter
/*     */ {
/*     */   public FormatterFactory.FormatType getType()
/*     */   {
/*  57 */     return FormatterFactory.FormatType.ICS;
/*     */   }
/*     */   
/*     */   public String[] getDefaultMimeTypes()
/*     */   {
/*  62 */     return new String[] { "text/calendar", "text/x-vcalendar" };
/*     */   }
/*     */   
/*     */   public Set<MailItem.Type> getDefaultSearchTypes()
/*     */   {
/*  67 */     return EnumSet.of(MailItem.Type.APPOINTMENT);
/*     */   }
/*     */   
/*     */   public void formatCallback(UserServletContext context) throws IOException, ServiceException
/*     */   {
/*  72 */     Iterator<? extends MailItem> iterator = null;
/*  73 */     List<CalendarItem> calItems = new ArrayList();
/*     */     
/*     */     try
/*     */     {
/*  77 */       long start = context.getStartTime();
/*  78 */       long end = context.getEndTime();
/*  79 */       boolean hasTimeRange = (start != -1L) && (end != -1L);
/*  80 */       iterator = getMailItems(context, start, end, 2147483647L);
/*     */       
/*     */ 
/*  83 */       while (iterator.hasNext()) {
/*  84 */         MailItem item = (MailItem)iterator.next();
/*  85 */         if ((item instanceof CalendarItem)) {
/*  86 */           CalendarItem calItem = (CalendarItem)item;
/*  87 */           if (hasTimeRange) {
/*  88 */             Collection<CalendarItem.Instance> instances = calItem.expandInstances(start, end, false);
/*  89 */             if (!instances.isEmpty())
/*  90 */               calItems.add(calItem);
/*     */           } else {
/*  92 */             calItems.add(calItem);
/*     */           }
/*     */         }
/*     */       }
/*     */     } finally {
/*  97 */       if ((iterator instanceof Formatter.QueryResultIterator)) {
/*  98 */         ((Formatter.QueryResultIterator)iterator).finished();
/*     */       }
/*     */     }
/*     */     
/* 102 */     String filename = context.itemPath;
/*     */     
/* 104 */     if (mayAttach(context)) {
/* 105 */       if ((filename == null) || (filename.length() == 0)) {
/* 106 */         filename = "contacts";
/*     */       }
/* 108 */       String requestFilename = context.req.getParameter("filename");
/* 109 */       if (requestFilename != null) {
/* 110 */         filename = requestFilename;
/*     */       } else {
/* 112 */         filename = filename.replaceAll("^\\W", "");
/*     */       }
/* 114 */       if (!filename.toLowerCase().endsWith(".ics"))
/* 115 */         filename = filename + ".ics";
/* 116 */       String cd = HttpUtil.createContentDisposition(context.req, "attachment", filename);
/* 117 */       context.resp.addHeader("Content-Disposition", cd);
/*     */     }
/* 119 */     HttpUtil.Browser browser = HttpUtil.guessBrowser(context.req);
/* 120 */     boolean useOutlookCompatMode = HttpUtil.Browser.IE.equals(browser);
/* 121 */     boolean needAppleICalHacks = HttpUtil.Browser.APPLE_ICAL.equals(browser);
/* 122 */     boolean htmlFormat = (!mayAttach(context)) && (HttpUtil.Browser.IE.equals(browser));
/*     */     
/* 124 */     context.resp.setCharacterEncoding("utf-8");
/*     */     String contentType;
/*     */     String contentType;
/* 127 */     if (htmlFormat) {
/* 128 */       contentType = "text/html"; } else { String contentType;
/* 129 */       if (mayAttach(context)) {
/* 130 */         contentType = "text/calendar";
/*     */       } else
/* 132 */         contentType = "text/plain";
/*     */     }
/* 134 */     context.resp.setContentType(contentType);
/*     */     
/* 136 */     OperationContext octxt = new OperationContext(context.getAuthAccount(), context.isUsingAdminPrivileges());
/* 137 */     FileBufferedWriter fileBufferedWriter = new FileBufferedWriter(context.resp.getWriter(), LC.calendar_ics_export_buffer_size.intValueWithinRange(0, 10485760));
/*     */     
/*     */     try
/*     */     {
/* 141 */       if (htmlFormat)
/* 142 */         fileBufferedWriter.write("<html><body><pre>");
/* 143 */       context.targetMailbox.writeICalendarForCalendarItems(fileBufferedWriter, octxt, calItems, (context.target != null) && ((context.target instanceof Folder)) ? (Folder)context.target : null, useOutlookCompatMode, true, needAppleICalHacks, true, htmlFormat, includeInlineAttaches(context));
/*     */       
/*     */ 
/*     */ 
/* 147 */       if (htmlFormat)
/* 148 */         fileBufferedWriter.write("</pre></body></html>");
/*     */     } finally {
/* 150 */       fileBufferedWriter.finish();
/*     */     }
/*     */   }
/*     */   
/*     */ 
/*     */ 
/*     */ 
/*     */   protected boolean includeInlineAttaches(UserServletContext context)
/*     */   {
/* 159 */     String inlineAttaches = context.req.getParameter("icalAttach");
/* 160 */     if (inlineAttaches == null) {
/* 161 */       return false;
/*     */     }
/* 163 */     return inlineAttaches.equals("inline");
/*     */   }
/*     */   
/*     */   public boolean supportsSave()
/*     */   {
/* 168 */     return true;
/*     */   }
/*     */   
/*     */   public void saveCallback(UserServletContext context, String contentType, Folder folder, String filename)
/*     */     throws UserServletException, ServiceException, IOException, ServletException
/*     */   {
/* 174 */     boolean continueOnError = context.ignoreAndContinueOnError();
/* 175 */     boolean preserveExistingAlarms = context.preserveAlarms();
/* 176 */     InputStream is = context.getRequestInputStream(-1L);
/* 177 */     String charset = "utf-8";
/* 178 */     String ctStr = context.req.getContentType();
/* 179 */     if (ctStr != null) {
/* 180 */       String cs = Mime.getCharset(ctStr);
/* 181 */       if (cs != null) {
/* 182 */         charset = cs;
/*     */       }
/*     */     }
/*     */     try {
/* 186 */       if (context.req.getContentLength() <= LC.calendar_ics_import_full_parse_max_size.intValue())
/*     */       {
/*     */ 
/*     */ 
/* 190 */         List<ZCalendar.ZVCalendar> icals = ZCalendar.ZCalendarBuilder.buildMulti(is, charset);
/* 191 */         IcsImportParseHandler.ImportInviteVisitor visitor = new IcsImportParseHandler.ImportInviteVisitor(context.opContext, folder, preserveExistingAlarms);
/* 192 */         Invite.createFromCalendar(context.targetAccount, null, icals, true, continueOnError, visitor);
/*     */ 
/*     */       }
/*     */       else
/*     */       {
/* 197 */         ZCalendar.ZICalendarParseHandler handler = new IcsImportParseHandler(context.opContext, context.targetAccount, folder, continueOnError, preserveExistingAlarms);
/*     */         
/*     */ 
/* 200 */         ZCalendar.ZCalendarBuilder.parse(is, charset, handler);
/*     */       }
/*     */     } finally {
/* 203 */       is.close();
/*     */     }
/*     */   }
/*     */ }


/* Location:              /home/mint/zimbrastore.jar!/com/zimbra/cs/service/formatter/IcsFormatter.class
 * Java compiler version: 7 (51.0)
 * JD-Core Version:       0.7.1
 */